<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
	</head>
	<body>
 
		18位身份证号<input type="text" id="cid" /><input type="button" name="jiaoyan" id="jiaoyan" value="校验" />
 
		<p id="msg"></p>
 
		<script type="text/javascript">
 
			document.getElementById("jiaoyan").onclick = function() {
 
				var cid = document.getElementById("cid").value;
 
				if(cid.length == 18) {
 
					var last = cid[17];
 
					var xishu = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
 
					var code = [1, 0, "X", 9, 8, 7, 6, 5, 4, 3, 2];
 
					var sum = 0;
 
					for(var i = 0; i < xishu.length; i) {
 
						sum = xishu[i] * cid[i];
 
					}
 
					console.log(sum % 11);
 
					if(last == code[sum % 11]) {
 
						document.getElementById("msg").innerHTML = "身份证号正确";
 
					} else {
 
						document.getElementById("msg").innerHTML = "身份证号错误";
 
					}
 
				}
 
			};
 
		</script>
 
	</body>
</html>
